package hello.entity;

import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

public class CallableCardThread implements Callable<List<String>> {
	
	private CardDefiniteEntity entity = null;
	
	public CallableCardThread(CardDefiniteEntity entity) {
		this.entity = entity;
	}

	@Override
	public List<String> call() throws Exception {
		return entity.getThirtyCard();
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException {
		Random random = new Random();
		int next = random.nextInt(51);
		System.out.println(next);
		
		CardDefiniteEntity entity = new CardDefiniteEntity();
		entity.clearAndReadInitCardList();
		ExecutorService executor = Executors.newFixedThreadPool(5);
		Future<List<String>> future = executor.submit(new CallableCardThread(entity));
		Future<List<String>> future1 = executor.submit(new CallableCardThread(entity));
		Future<List<String>> future2 = executor.submit(new CallableCardThread(entity));
		Future<List<String>> future3 = executor.submit(new CallableCardThread(entity));
				
		List<String> list = future.get(30, TimeUnit.SECONDS);
		List<String> list1 = future1.get(30, TimeUnit.SECONDS);
		List<String> list2 = future2.get(30, TimeUnit.SECONDS);
		List<String> list3 = future3.get(30, TimeUnit.SECONDS);
		
		System.out.println( list.stream().sorted( (n1,n2)->n1.compareTo(n2)).collect(Collectors.toList()) );
		System.out.println( list1.stream().sorted( (n1,n2)->n1.compareTo(n2)).collect(Collectors.toList()) );
		System.out.println( list2.stream().sorted( (n1,n2)->n1.compareTo(n2)).collect(Collectors.toList()) );
		System.out.println( list3.stream().sorted( (n1,n2)->n1.compareTo(n2)).collect(Collectors.toList()) );
				
	}

}
